<template>
  <div class="app-container" v-loading="loading">
    <!-- 添加或修改菜谱对话框 -->

    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="80px"
      class="el-form"
    >
      <div class="disp-from-flxe">
        <el-form-item label="标题" prop="">
          <el-input v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item label="行业分类" prop="cateId">
          <el-select v-model="form.cateId" filterable>
            <el-option
              v-for="(item, index) in caetArr"
              :key="index"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </div>

      <div class="disp-from-flxe">
        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker
            v-model="form.startTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="排序">
          <el-input-number
            v-model="form.sort"
            :min="1"
            @change="cheNumMoney"
          ></el-input-number>
        </el-form-item>
      </div>

      <div class="disp-from-flxe">
        <el-form-item label="采购清单">
          <el-table border :data="form.params.details">
            <!-- <el-table-column type="selection" width="55" align="center" /> -->
            <el-table-column
              label="序号"
              align="center"
              type="index"
              width="50"
            />

            <el-table-column label="名称" align="center" prop="typeName">
            </el-table-column>
            <el-table-column label="规格" align="center" prop="specName">
            </el-table-column>
            <el-table-column label="单位" align="center" prop="brandName">
            </el-table-column>

            <el-table-column label="数量" align="center" prop="allNum">
            </el-table-column>

            <el-table-column label="最高报价" align="center">
              <template slot-scope="scope">
                <el-input-number
                  v-model="scope.row.price"
                  :min="0"
                  :precision="2"
                  @change="(e) => cheNum(e, scope)"
                />
              </template>
            </el-table-column>
          </el-table>
        </el-form-item>
      </div>
      <div class="disp-from-flxe">
        <el-form-item label="内容">
          <Editor class="editcon" v-model="form.content" :height="300"></Editor>
        </el-form-item>
      </div>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="$emit('cancel')">取 消</el-button>
      <el-button type="primary" @click="submitForm">确 定</el-button>
    </div>
  </div>
</template>
      
      <script>
import { getComputeList, addQroject } from "@/api/system/quantify";
import {
  getDeliveryInfo,
  addDelivery,
  editDelivery,
} from "@/api/system/procure";
import { getFormatCateData } from "@/api/system/cate";
import { Base64 }  from 'js-base64'
export default {
  name: "addMenu",
  props: {
    bidData: {
      type: Object,
      default: {},
    },
  },
  data() {
    return {
      loading: true,
      typeArr: [], //类型
      brandArr: [], //单位
      specArr: [], //规格
      schoolArr: [], //学校
      entArr: [], //供应商
      caetArr: [], //类目
      caseArrs: [
        { val: 0, lab: "小学" },
        { val: 1, lab: "初中" },
      ],
      // 表单参数
      form: {
        id: undefined,
        cateId: undefined,
        startTime: undefined,
        sort: 0,
        win: 0,
        content: "",
        params: {
          details: [
            {
              allNum: undefined, //数量
              typeId: undefined, //分类
              specId: undefined, //规格
              brandId: undefined, //单位
              id: undefined,
            },
          ],
        },
      },
      // 表单校验
      rules: {
        title: [{ required: true, message: "请输入标题", trigger: "blur" }],
        cateId: [
          {
            required: true,
            message: "请选择类目",
            trigger: ["blur", "change"],
          },
        ],
        startTime: [
          {
            required: true,
            message: "请选择开始时间",
            trigger: ["blur", "change"],
          },
        ],
        // param
      },
    };
  },
  created() {
    this.show();
    
    // console.log(Base64.encode('11'))
    //   console.log(this.mId)
    //   console.log('this.mId')
    // console.log(this.mId)
  },
  methods: {
    cheNumMoney(e) {
      if (e == undefined) {
        e = 0.0;
        this.form.sort = 0;
        // console.log(this.form.money)
        this.$forceUpdate();
        // form.money
      }
    },
    chType(e, scope) {
      console.log(e);
      console.log(scope);
    },
    cheNum(e, scope) {
      if (e == undefined) {
        scope.row.num = 0;
      }
    },
    show() {
      //类目
      getFormatCateData({ pageNum: -1, pageSize: -1 }).then((response) => {
        this.caetArr = response.rows;
      });

      // id: undefined,
      //   cateId: undefined,
      //   startTime: undefined,
      //   sort: 0,
      //   win:0,
      //   content:'',
      //   params: {
      //     details: [
      //       {
      //         allNum: undefined, //数量
      //         typeId: undefined, //分类
      //         specId: undefined, //规格
      //         brandId: undefined, //单位
      //         id: undefined,
      //       },
      //     ],
      //   },

      getComputeList({
        pageNum: -1,
        pageSize: -1,
        ids: this.bidData.ids.toString(),
      }).then((response) => {
        // var res = response.data;
        // console.log(res)
        this.form.params.details = [];
        response.rows.forEach((item, index) => {
          this.form.params.details.push({
            id: item.id,
            allNum: item.allNum,
            typeId: item.typeId,
            specId: item.specId,
            brandId: item.brandId,
            typeName: item.typeName,
            specName: item.specName,
            brandName: item.brandName,
            price: 0,
          });
        });
        // this.form.params.details = response.rows
        // this.form.params.details.forEach((item,index)=>{
        //   item.price = 0
        // })

        this.loading = false;
        //     this.form = {
        //       id: res.id,
        //       schoolId: res.schoolId,
        //       userId: res.userId,
        //       sendTime: res.sendTime,
        //       money: res.money,
        //       params: {
        //         details: res.details,
        //       },
        //     };
      });
    },
    delParams(scope) {
      //   console.log(scope);
      this.form.params.details.splice(scope.$index, 1);
    },
    addParams(scope) {
      this.form.params.details.push({
        typeId: this.typeArr[0].id, //分类id
        specId: this.typeArr[0].specId, //规格id
        brandId: this.typeArr[0].brandId, //单位id
        price: 0, //单价
        num: 0, //采购数量
        money: 0, //总价
        remark: undefined, //备注
        jMoney: 0, //收货均价
        shouNum: 0, //已收数量
        weiNum: 0, //未收货数量
        id: undefined,
      });
      //   console.log(scope);
    },
    cancel() {
      this.$emit("cancel");
    },
    /** 提交按钮 */
    submitForm: function () {
      //   try {
      //     arr.forEach(function (value, index, array) {
      //       this.params.details.forEach((e, i) => {
      //         if (i == 1) {
      //           // break;
      //         }
      //       });
      //     });
      //   } catch (e) {
      //     if (e.message !== "Stop Iteration") throw e; // 只捕获我们的异常，其他异常继续抛出
      //   }
      this.$refs["form"].validate((valid) => {
        if (valid) {
          //   return;
          // console.log("111");

          // data
          var data = [];
          this.form.params.details.forEach((item, index) => {
            data.push({
              id: item.id,
              allNum: item.allNum,
              typeId: item.typeId,
              specId: item.specId,
              brandId: item.brandId,
              price: item.price,
            });
          });
          addQroject({
            // id: undefined,
            title: this.form.title,
            cateId: this.form.cateId,
            startTime: this.form.startTime,
            sort: this.form.sort,
            win: this.form.win,
            content: Base64.encode(this.form.content),
            params: {
              details: data,
            },
          }).then((response) => {
            this.$modal.msgSuccess("新增成功");
            this.$emit("submitForm");
          });
        }
      });
    },
  },
};
</script>
      